library(gridExtra)
library(readstata13)
library(ggplot2)
library(gridExtra)
library(foreign)
library(MASS)
library(Matrix)
library(lattice)
library(rdd)
library(foreign)
library(ri)
library(scales)

rm(list = ls())

# Set working directory. Please set your own
setwd("~/Desktop/PSRM Replication")

##################################
#Figure 1, Histogram, Zig-zagging#
##################################

fran <- read.dta13("Data/anes_cumulative.dta")

theme_base1 <- 
  theme_minimal(base_size=14)  + 
  theme(legend.position=c(0.15, .9),  legend.key.size = unit(1,"line"),
        axis.text=element_text(size=14),axis.title.x=element_text(size=14),axis.title.y=element_text(size=14),
        plot.title = element_text(size=14, hjust= 0.5))

fran2<-fran[fran$first_pres<2, ]
fran2$fp<-as.factor(fran2$first_pres)



main<-ggplot(fran2, aes(x=birthyr,           
                        fill=factor(first_pres))) +            
  geom_histogram(binwidth = 1)+ 
  scale_fill_manual(name="First Eligible\nElection",           
                    labels=c("Midterm","Presidential"),
                    values=c("black", "gray60"))  + xlab("Year of Birth") + 
  theme_base1 + 
  ylab("Density") + 
  geom_hline(yintercept=0, colour="white", size=1) 
main
ggsave("Graphs/histogram.pdf", width = 22, height = 18, units = "cm")  


fran2<-fran[fran$first_pres<2, ]
fran2$fp<-as.factor(fran2$first_pres)


theme_base2 <- 
  theme_minimal(base_size=14)  + 
  theme(legend.position=c(.85, .9),  legend.key.size = unit(1,"line"),
        axis.text=element_text(size=14),axis.title.x=element_text(size=14),axis.title.y=element_text(size=14),
        plot.title = element_text(size=14, hjust= 0.5))

main<-ggplot(fran2,aes(x=age, fill=fp)) + geom_density(alpha=0.25)+ylab("Density") + theme_base2 +
  scale_fill_manual(name="First Eligible\nElection",labels=c("Midterm","Presidential"), values=c("blue", "red")) + xlab("Age") 
main
ggsave("Graphs/densities.pdf", width = 22, height = 18, units = "cm")  